<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace Model\LogExamBundle\Model;
use Base\BaseModel;
use Model\ExamBundle\Model\ExamModel;
use Model\LogExamBundle\Entity\LogExam;
use Model\LogExamBundle\Entity\LogExamQuestion;
use Model\LogExamBundle\Setting\LogExamSetting;

class LogExamModel extends BaseModel{

    /*
     * 列表基本设置【通用模块必须开发】
     */
    public function listSettingDefault()
    {
        $settings = new LogExamSetting($this->getDoctrine());
        return $settings->listConfig();
    }

    /*
     * 为后台获取所有文章分类【通用模块必须开发】
     */
    /**
     * @param array $params $param[textKey]=textKey , $param[conditions][table][field][eq|gt|lt...]=value , $param[orderBy][table][field]=DESC|ASC
     * @param int $offset
     * @param int $perPage
     * @return array
     */
    public function getAdminLists($params ,$offset = 0 , $perPage = 10)
    {
        $qb = $this->getQueryFromParam($params);
        $lists = $this->getAllResult($qb,$offset,$perPage);
        $total = $this->countAllResult($qb);

        return array(
            "total" => $total ? $total : 0,
            "data"  => $lists
        );
    }

    public function chkExam4User($examId , $userId)
    {
        $rep = $this->getDoctrine()->getRepository("ModelLogExamBundle:LogExam");
        $curLogExam = $rep->findBy(array(
            "examId"  => $examId,
            "userId"   => $userId,
            ));
        if (!$curLogExam) {
            //找不到对应记录
            return false;
        } else {
            //记录可以找到
            return $curLogExam;
        }
    }
    public function chkExamQuestion4User($examId , $questionId , $userId)
    {
        $rep = $this->getDoctrine()->getRepository("ModelLogExamBundle:LogExamQuestion");
        $curLogExamQuestion = $rep->findBy(array(
            "examId"        => $examId,
            "userId"        => $userId,
            "questionId"    => $questionId,
            ));
        if (!$curLogExamQuestion) {
            //找不到对应记录
            return false;
        } else {
            //记录可以找到
            return $curLogExamQuestion;
        }
    }

    public function startExam($examId , $userId)
    {
        if (!$logExam = $this->chkExam4User($examId,$userId)) {
            $logExam = new LogExam();
        } else {
            return false;
        }
        $em = $this->getDoctrine();

        $logExam->setExamId($examId);
        $logExam->setUserId($userId);
        $logExam->setStartTime(time());
        $logExam->setScore(0);
        $logExam->setUserEndTime(0);
        $logExam->setTimeSpend(0);

        //设置答题结束的最后期限
        $examModel = new ExamModel($this->getDoctrine());

        if (!$exam = $examModel->getExamOne($examId)) {
            return false;
        }
        $timeLimit = $exam["totalTime"] * 60 + $logExam->getStartTime();
        $logExam->setEndTime($timeLimit);

        $em->persist($logExam);
        $em->flush();
        return $logExam;
    }

    public function endExam($examId , $userId)
    {
        if (!$logExam = $this->chkExam4User($examId,$userId)) {
            $logExam = new LogExam();
        } else {
            return false;
        }
        $em = $this->getDoctrine();

        $logExam->setExamId($examId);
        $logExam->setUserId($userId);
        $logExam->setStartTime(time());
        $logExam->setEndTime(0);
        $logExam->setScore(0);
        $logExam->setTimeSpend(0);
        $logExam->setUserEndTime(0);
        $em->persist($logExam);
        $em->flush();
        return $logExam;
    }

    public function startQuestion($examId , $questionId , $userId)
    {
        if (!$logExamQuestion = $this->chkExamQuestion4User($examId , $questionId , $userId)) {
            $logExamQuestion = new LogExamQuestion();
        } else {
            return false;
        }
        $em = $this->getDoctrine();

        $logExamQuestion->setExamId($examId);
        $logExamQuestion->setUserId($userId);
        $logExamQuestion->setQuestionId($questionId);
        $logExamQuestion->setScore(0);
        $logExamQuestion->setTimeSpend(0);
        $logExamQuestion->setIsDifficult(0);
        $logExamQuestion->setIsRight(0);
        $logExamQuestion->setIsDone(0);

        $em->persist($logExamQuestion);
        $em->flush();
        return $logExamQuestion;
    }



}